local ViewBase = require("app.ui.ViewBase")
local CameraDemo = class("CameraDemo", ViewBase)

local CameraType = {
    FreeCamera = 0,
    FirstCamera = 1,
    ThirdCamera = 2
}

function CameraDemo:ctor()
    -- body
    CameraDemo.super.ctor(self)
    self:init()
end

function CameraDemo:init()
    -- body
    self._layer3D = nil
    self._cameraType = CameraType.FreeCamera
    self._camera = nil
    self._shader = nil
    self._state = nil
end

function CameraDemo:setEventListener()
    local listener = cc.EventListenerTouchAllAtOnce:create()
    listener:registerScriptHandler(
        function(touches, event)
            logWarn("EventListenerTouchAllAtOnce")
            if #touches == 1 then
                local touch = touches[1]
                local prelocation = touch:getPreviousLocationInView()
                local location = touch:getLocationInView()
                local newPos = cc.p(prelocation.x - location.x, prelocation.y - location.y)
                if self._cameraType == CameraType.FreeCamera then
                    local transformMat = self._camera:getNodeToWorldTransform()
                    local cameraDir = {x = -transformMat[9], y = -transformMat[10], z = -transformMat[11]}
                    cameraDir = cc.vec3normalize(cameraDir)
                    -- cameraDir.y = 0

                    transformMat = self._camera:getNodeToWorldTransform()
                    local cameraRightDir = {x = transformMat[1], y = transformMat[2], z = transformMat[3]}
                    cameraRightDir = cc.vec3normalize(cameraRightDir)
                    -- cameraRightDir.y = 0
                    dump(cameraDir)
                    local cameraPos = self._camera:getPosition3D()
                    cameraPos = {
                        x = cameraPos.x - cameraDir.x * newPos.y * 0.1,
                        y = cameraPos.y - 1* newPos.y * 0.1,
                        z = cameraPos.z - cameraDir.z * newPos.y * 0.1
                    }
                    cameraPos = {
                        x = cameraPos.x + cameraRightDir.x * newPos.x * 0.1,
                        y = cameraPos.y,
                        z = cameraPos.z + cameraRightDir.z * newPos.x * 0.1
                    }
                    dump(cameraPos)
                    logWarn("_____________________________________")
                    self._camera:setPosition3D(cameraPos)
                end
            end
        end,
        cc.Handler.EVENT_TOUCHES_MOVED
    )

    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end

function CameraDemo:createMenu()
end

function CameraDemo:createLayer3D()
    -- body
    local s = cc.Director:getInstance():getWinSize()

    local layer3D = cc.Layer:create()
    self:addChild(layer3D, 10)
    self._layer3D = layer3D

    self._sprite3D1 = ccui.ImageView:create()
    self._sprite3D1:loadTexture("ui/chapter/background/aaa.jpg")
    self._layer3D:addChild(self._sprite3D1)
    self._sprite3D1:setPosition3D(cc.vec3(0, 0, 0))
    self._sprite3D1:setScale(1.0)
    self._sprite3D1:setRotation3D(cc.vec3(-20, 0, 0))

    if self._camera == nil then
        self._camera = cc.Camera:createPerspective(60, s.width / s.height, 1, 5000)
        self._camera:setCameraFlag(cc.CameraFlag.USER1)
        self._camera:setPosition3D(cc.vec3(0, 0, 900))
        self._camera:lookAt(cc.vec3(0, 0, 0), cc.vec3(0, 1, 0))

        self._layer3D:addChild(self._camera)
    end

    self._layer3D:setCameraMask(2)
end

function CameraDemo:onEnter()
    cc.Director:getInstance():setClearColor(cc.c4f(0.5, 0.5, 0.5, 1))
    self:setEventListener()
    self:createMenu()
    self:createLayer3D()
end

function CameraDemo:onExit()
    cc.Director:getInstance():setClearColor(cc.c4f(0, 0, 0, 1))
    if nil ~= self._camera then
        self._camera = nil
    end
end

function CameraDemo:title()
    return "Fog Test Demo"
end

function CameraDemo:subtitle()
    return ""
end

return CameraDemo
